package com.xflib.framework.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class StrUtil {

    public static class QueryString {
        public static Map<String, String> parse(final String queryString) {
            Map<String, String> result = new HashMap<>();
            String myQueryString = queryString;
            if (queryString.startsWith("?")) myQueryString = myQueryString.substring(1);
            Arrays.asList(myQueryString.split("&")).forEach(kv -> {
                result.put(kv.split("=")[0], kv.split("=")[1]);
            });
            return result;
        }

        public static String stringify(Map<String, String> map) {
            StringBuilder sb=new StringBuilder("?");
            map.forEach((k,v)->sb.append("k").append("=").append(v).append("&"));
            return sb.substring(0,sb.length()-1);
        }
    }

    /**
     * 中杠转驼峰
     * @param input
     * @return
     */
    public static String camelize(String input) {
        for (int i = 0; i < input.length(); i++) {
            if(input.substring(i, i+1).equals("-")) {
                input.replace("-", "");
                input = input.substring(0, i) + input.substring(i+1, i+2).toUpperCase() + input.substring(i+2);
            }
            if(input.substring(i, i+1).equals(" ")) {
                input.replace(" ", "");
                input = input.substring(0, i) + input.substring(i+1, i+2).toUpperCase() + input.substring(i+2);
            }
        }
        return input;
    }
}
